package cn.ipanel.bigdata.dw.dim.phoenix

import cn.ipanel.bigdata.boot.source.genre.Phoenix
import cn.ipanel.bigdata.dw.DetailColumns
import cn.ipanel.bigdata.dw.dws.HBASE_NAMESPACE
import cn.ipanel.bigdata.utils.Dictionary.{F_DATE, F_STATUS}
import org.apache.spark.sql.{Dataset, Row}

/**
 * @author lzz
 * @environment IntelliJ IDEA 2020.3.1
 * @projectName bigdata_panyu   番禺大数据，区域数据
 * @date 2023/11/14 16:15
 * @description:
 */
protected[phoenix] object IOTTown extends Phoenix(HBASE_NAMESPACE, "t_areacode_town_panyu") with IotArea with DetailColumns {

  override def getTBColumns: Seq[String] = Seq(
    F_CODE, F_NAME, F_PARENT_CODE, F_LEVEL, F_STATUS, F_CREATE_TIME, F_UPDATE_TIME
  )

  override def buildTable: String = {
    s"""
       |CREATE TABLE IF NOT EXISTS $getDBName.$getTBName(
       |$F_CODE BIGINT NOT NULL
       |, $F_NAME VARCHAR
       |, $F_PARENT_CODE BIGINT
       |, $F_LEVEL TINYINT
       |, $F_STATUS TINYINT
       |, $F_CREATE_TIME VARCHAR
       |, $F_UPDATE_TIME VARCHAR
       |CONSTRAINT PK PRIMARY KEY($F_CODE)
       |) SALT_BUCKETS = $saltBuckets;
       |""".stripMargin
  }

  override def emptyTable: Dataset[_] = {
    import IMPLICITS._
    spark.createDataset(spark.sparkContext.emptyRDD[IOTNeighborhoodTable])
  }

  case class IOTNeighborhoodTable(var f_code: Long
                                  , var f_name: String
                                  , var f_parent_code: Long
                                  , var f_level: Int
                                  , var f_status: Int
                                  , var f_create_time: String
                                  , var f_update_time: String
                         )

  object IOTNeighborhoodTable {
    def apply(row: Row): IOTNeighborhoodTable = {
      IOTNeighborhoodTable(row.getAs[Long](F_CODE)
        , row.getAs[String](F_NAME)
        , row.getAs[Long](F_PARENT_CODE)
        , row.getAs[Int](F_LEVEL)
        , row.getAs[Int](F_STATUS)
        , row.getAs[String](F_CREATE_TIME)
        , row.getAs[String](F_UPDATE_TIME)
      )
    }
  }
}